/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.tv.settings.device.display;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.media.AudioManager;
import android.os.Bundle;
import android.provider.Settings;
import android.content.res.Configuration;
import android.app.ActivityManagerNative;

import com.android.tv.settings.R;
import com.android.tv.settings.dialog.DialogFragment;
import com.android.tv.settings.dialog.DialogFragment.Action;

import android.os.SystemProperties;
import android.os.RemoteException;

import java.util.ArrayList;

/**
 * Activity that allows the enabling and disabling of ProjMode effects.
 */
public class ProjFontActivity extends Activity implements Action.Listener {

    private static final String PREFERENCE_KEY = "display";
    private static final String ACTION_FONT_SMALL = "font_1.0";
    private static final String ACTION_FONT_MID = "font_1.1";
    private static final String ACTION_FONT_LARGE = "font_1.3";
    private static final String ACTION_FONT_HUGE = "font_1.5";
    private DialogFragment mDialogFragment;

    private static final Configuration mCurConfig = new Configuration();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mDialogFragment = new DialogFragment.Builder()
                .title(getString(R.string.font_size))
                .breadcrumb(getString(R.string.device_display))
                .iconResourceId(getIconResource(getContentResolver()))
                .iconBackgroundColor(getResources().getColor(R.color.icon_background))
                .actions(getActions()).build();
        DialogFragment.add(getFragmentManager(), mDialogFragment);
    }

    private ArrayList<Action> getActions() {
        String ProjModeAreOn = getProjectFont();
        ArrayList<Action> actions = new ArrayList<Action>();
        actions.add(new Action.Builder()
                .key(ACTION_FONT_SMALL)
                .title(getString(R.string.font_small))
                .checked(ACTION_FONT_SMALL.contains(ProjModeAreOn))
                .checkSetId(1)
                .build());
        actions.add(new Action.Builder()
                .key(ACTION_FONT_MID)
                .title(getString(R.string.font_normal))
                .checked(ACTION_FONT_MID.contains(ProjModeAreOn))
                .checkSetId(1)
                .build());
        actions.add(new Action.Builder()
                .key(ACTION_FONT_LARGE)
                .title(getString(R.string.font_large))
                .checked(ACTION_FONT_LARGE.contains(ProjModeAreOn))
                .checkSetId(1)
                .build());
        actions.add(new Action.Builder()
                .key(ACTION_FONT_HUGE)
                .title(getString(R.string.font_huge))
                .checked(ACTION_FONT_HUGE.contains(ProjModeAreOn))
                .checkSetId(1)
                .build());
        return actions;
    }

    @Override
    public void onActionClicked(Action action) {
        if (ACTION_FONT_SMALL.equals(action.getKey())) {
            setProjectFont("0.9");
        } else if (ACTION_FONT_MID.equals(action.getKey())) {
            setProjectFont("1.1");
        } else if (ACTION_FONT_LARGE.equals(action.getKey())) {
            setProjectFont("1.3");
        } else if (ACTION_FONT_HUGE.equals(action.getKey())) {
            setProjectFont("1.5");
        }
        mDialogFragment.setIcon(getIconResource(getContentResolver()));
    }

    public static String getPreferenceKey() {
        return PREFERENCE_KEY;
    }

    public static int getIconResource(ContentResolver contentResolver) {
        return R.drawable.ic_settings_display;//need to do replace ico Blues
    }

    private static void setProjectFont(String mValue) {
        try {
            mCurConfig.fontScale = Float.parseFloat(mValue);
            ActivityManagerNative.getDefault().updatePersistentConfiguration(mCurConfig);
        } catch (RemoteException e) {

        }
    }

    private static String getProjectFont() {
        try {
            mCurConfig.updateFrom(ActivityManagerNative.getDefault().getConfiguration());
        } catch (RemoteException e) {
        }
        return String.valueOf(mCurConfig.fontScale);
    }


    public static int getProjectFontInt() {
        String tmp = getProjectFont();
        if (ACTION_FONT_SMALL.contains(tmp)) {
            return R.string.font_small;
        } else if (ACTION_FONT_MID.contains(tmp)) {
            return R.string.font_normal;
        } else if (ACTION_FONT_LARGE.contains(tmp)) {
            return R.string.font_large;
        } else if (ACTION_FONT_HUGE.contains(tmp)) {
            return R.string.font_huge;
        }
        return R.string.font_small;
    }
}
